<?php


function renderTops($type,$page){
	global $wgMemc, $wgParser, $wgTitle, $wgOut;

	$rowsByPage=10;
	
	$db=wfGetDB( DB_MASTER );

	switch($type){
		case 'a':
			$sql = "select s.Count as w, t.Artist, t.Title as SugTitle, t.Year as SugYear, s.int1  sources, s.int2 files, s.int3 tags, s.float1 weight, s.firstSeen from searches s inner join ss_tags t on s.idtags=t.idtags where s.query ='groupby=attr:artistkey;limit=$rowsByPage;offset=".(($page-1)*$rowsByPage).";select=idtags as idtags, firstSeen as firstSeen, sum(10/(firstSeen+1)*(CountTagsOk+1)) as Count, sum(totalcount) as int1,sum(countTagsOk) as int2, sum(1) as int3, avg(weightTags) as float1;groupsort=Count desc;sort=expr:CountTagsOk*100+CountFiles;maxmatches=1000000;filter=type,1';";
			break;
		case 'b':
			$sql = "select s.Count as w, t.Artist, t.Album, t.Title as SugTitle, s.int1  sources, s.int2 files, s.int3 tags, s.float1 weight, s.firstSeen from searches s inner join ss_tags t on s.idtags=t.idtags where s.query ='groupby=attr:topAlbumkey;offset=1;limit=$rowsByPage;offset=".(($page-1)*$rowsByPage+1).";select=idtags as idtags, firstSeen as firstSeen, sum(10/(firstSeen+1)*(CountTagsOk+1)) as Count, sum(totalcount) as int1,sum(countTagsOk) as int2, sum(1) as int3, avg(weightTags) as float1;groupsort=Count desc;sort=expr:CountTagsOk*100+CountFiles;maxmatches=1000000;filter=type,1';";
			break;
		case 's':
			$sql = "select s.Count as w, t.Artist, t.Title, s.int1 sources, s.int2 files, s.int3 tags, s.float1 weight, s.firstSeen from searches s inner join ss_tags t on s.idtags=t.idtags where s.query ='groupby=attr:songKey;limit=$rowsByPage;offset=".(($page-1)*$rowsByPage).";select=idtags as idtags, firstSeen as firstSeen, sum(10/(firstSeen+1)*(CountTagsOk+1)) as Count, sum(totalcount) as int1,sum(countTagsOk) as int2, sum(1) as int3, avg(weightTags) as float1;groupsort=Count desc;sort=expr:CountTagsOk*100+CountFiles;maxmatches=1000000;filter=type,1';";
			break;
	}
			
	$output .= "<ol>";
	$res = $db->query( $sql );
      	
	$max_pop = $wgMemc->get("top-$type-maxpop", 1);

	$i = (($page-1)*$rowsByPage)+1;
	while ($row = $db->fetchObject( $res ))
	{		
		switch($type) {
			case 'a':
				$wikiText=ss_getArticleRawText("Artist:".$row->Artist);
				$wikiText = mb_ereg_replace('{{ArtistInfo', '{{ArtistTop', $wikiText);
				break;
			case 'b':
				$wikiText=ss_getArticleRawText("Release:".normalize($row->Artist).'/'.$row->Album);
				$wikiText = mb_ereg_replace('{{AlbumTop', '{{AlbumTop', $wikiText);
				break;
			case 's':
				$wikiText=ss_getArticleRawText("Song:".normalize($row->Artist).'/'.$row->Title);
				$wikiText = mb_ereg_replace('{{SongInfo', '{{SongTop', $wikiText);
				break;
		}

		$popts = $wgOut->parserOptions(); 	
		$wikiText = truncate($wgParser->parse($wikiText, $wgTitle, $popts)->getText(), 450, "...", true);
		$titleclass = '';
		if (preg_match("/.*<img.*/ui", $wikiText)>0) $titleclass = ' tops-title-pic';
		$output .="<li><div class='tops-title$titleclass'>";

		if ($i==1) {$wgMemc->set("top-$type-maxpop", $row->w); $max_pop = $row->w;}

		//$dbr->freeResult($res);
		$output.=$i.' <a href="'.createURL(URL_ARTIST, urldecode($row->Artist)).'">'.$row->Artist.'</a>';
		if ($row->Album!="") $output.=" - <a href='".createURL(URL_RELEASE, array(urlencode($row->Artist),urlencode($row->Album)))."'>$row->Album</a>";
		if ($row->Title!="") $output.=" - <a href='".createURL(URL_SONG, array(urlencode($row->Artist),urlencode($row->Title)))."'>$row->Title</a>";
		$output .= "<span style='font-size:9px;position:absolute;color:transparent'>";
		if ($row->sources!="") $output.=" sources:$row->sources";
		if ($row->files!="") $output.=" filesOk:$row->files";
		if ($row->tags!="") $output.=" tags:$row->tags";
		if ($row->weight!="") $output.=" weight:$row->weight";
		if ($row->firstSeen!="") $output.=" firstSeen:$row->firstSeen";
		if ($row->w!="") $output.=" Total:$row->w";
		$output .= "</span>";

		//if($textoArtist!=""){
			$output .= '<div class="button-grey"><div class="button-grey-start"></div>';
			$output .= '<a href="lalal">[+] more...</a>';
			$output .= '<div class="button-grey-end"></div></div>';
		//}
		$output .= "<div class='tops-popularity'>".round($row->w/$max_pop,2)."</div>";
		$output.='</div><div class="tops-excerpt">'.$wikiText;
		if ($row->SugTitle!="") $output .= wfMsg('popular_song').": <a href='".createURL(URL_SONG, array(urlencode($row->Artist),urlencode($row->SugTitle)))."'>$row->SugTitle</a>";
		if ($row->SugYear>1901 && $row->SugYear<date('YYYY')) $output .= " ($row->SugYear)";
		$output .='</div>';
		$output.='</li>';
		
		$i++;
	}
	$output.= "</ol>";
	$output.="<div id='panel-topArtist".($page+1)."'></div>";

	$db->freeResult( $res );
	return $output;
}	
?>
